package com.bitcola.security.admin.biz;

import com.bitcola.security.admin.mapper.ColaFinancialRecordsMapper;
import com.bitcola.security.admin.vo.FinancialRecordsVO;
import com.bitcola.security.admin.vo.Serie;
import com.bitcola.security.common.msg.AppResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.*;

/**
 * @date 2019/11/17 13:57
 */
@Service
public class ColaFinancialRecordsBiz {

    @Autowired
    private ColaFinancialRecordsMapper colaFinancialRecordsMapper;

    public AppResponse statistics(Long startTime, Long endTime, String interval) {
        List<LinkedHashMap<String, Object>> mapList = colaFinancialRecordsMapper.financialRecords(startTime, endTime, interval);
        FinancialRecordsVO vo = new FinancialRecordsVO();
        vo.setLegend(Arrays.asList("提现", "充值", "下单", "成交", "手续费", "活动奖励", "系统奖励"));
//        vo.setLegend(Arrays.asList("Withdraw", "Deposit", " tradeIn", "tradeOut", "feeOut", "Activity reward", "System reward"));
        for (String name : vo.getLegend()) {
            Serie serie = new Serie();
            serie.setName(name);
            vo.getSeries().add(serie);
        }
//        步长
        for (LinkedHashMap<String, Object> mapTime : mapList) {
            String tdate = mapTime.get("timedate").toString();
            if (vo.getXAxis().contains(tdate)) {
                continue;
            } else {
                vo.getXAxis().add(tdate);
            }
        }
//        根据步长(顺序)和name去找
        int serieSize = vo.getSeries().size();
        for (String intervalStr : vo.getXAxis()) {
            for (int j = 0; j < serieSize; j++) {
                Serie serie = vo.getSeries().get(j);
                int size = mapList.size();
//            标记位-找到与否
                boolean flag = false;
                int startIndex = 0;//集合起始下标
                for (int i = 0; i < size; i++) {
                    LinkedHashMap<String, Object> map = mapList.get(i);
                    String timedate = map.get("timedate").toString();
                    String ttype = map.get("ttype").toString();
//                    某时刻的某条记录的统计数-只对应一条，跳出
                    if (serie.getLegendName().equals(ttype) && intervalStr.equals(timedate)) {
                        int count = Integer.parseInt(map.get("count").toString());
                        BigDecimal sum = new BigDecimal(map.get("sum").toString());
                        flag = true;
//                        serie.getData().add(count);
                        serie.getData().add(sum.abs());
                        startIndex++;
                        break;
                    }
//                    找到最后还找不到-默认值0
                    if (i == size - 1 && !flag) {
                        serie.getData().add(BigDecimal.valueOf(0));
                    }
                }
            }
        }
        return new AppResponse().data(vo);
    }
}
